<?php

declare(strict_types=1);

namespace app\api\controller\v1;

use Throwable;
use app\common\Frontend;
use app\common\model\Order as ModelOrder;
use app\common\model\PublicStatus;
use Godruoyi\Snowflake\Snowflake;
use think\Request;

class Order extends Frontend
{
    protected $snowflake;
    protected array $noNeedLogin = ['subscribeCreateOrder'];
    protected $model;
    public function initialize(): void
    {
        parent::initialize();
        $this->snowflake = new Snowflake();
        $this->model = new ModelOrder();
    }

    //订单列表
    public function orderList(): void
    {
        $limit      = $this->request->request('limit', 10);
        $res        = $this->model->with(['invoice'])->where('user_id', $this->auth->id)
            ->order('create_time desc')
            ->paginate($limit)->each(function ($item, $key) {
                $item['status_text'] = PublicStatus::$status[$item['status']] ?? '其他';
                return $item;
            });

        $this->success('', [
            'list'  => $res->items(),
            'total' => $res->total(),
        ]);
    }

    //创建订单 
    public function createOrder(): void
    {
        if ($this->request->isPost()) {
            $data = $this->request->param('createOrderData');
            if (!$data) {
                $this->error('参数不能为空');
            }
            $order_no = date('YmdHis') . $this->snowflake->id();
            $data['user_id'] = $this->auth->id;
            $data['order_no'] = $order_no;
            $data['expire_at'] = time() + 120; //2分钟后过期
            $result = false;
            $this->model->startTrans();
            try {
                $result = $this->model->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success('创建成功', [
                    'order_no' => $order_no
                ]);
            } else {
                $this->error(('未添加任何行'));
            }
        }
        $this->error(('参数错误'));
    }


    //创建订单 
    public function subscribeCreateOrder(): void
    {
        if ($this->request->isPost()) {
            $data = $this->request->param('createOrderData');
            if (!$data) {
                $this->error('参数不能为空');
            }
            $order_no = date('YmdHis') . $this->snowflake->id();
            $data['user_id'] = 0;
            $data['order_no'] = $order_no;
            $data['expire_at'] = time() + 120; //2分钟后过期
            $result = false;
            $this->model->startTrans();
            try {
                $result = $this->model->save($data);
                $this->model->commit();
            } catch (Throwable $e) {
                $this->model->rollback();
                $this->error($e->getMessage());
            }
            if ($result !== false) {
                $this->success('创建成功', [
                    'order_no' => $order_no
                ]);
            } else {
                $this->error(('未添加任何行'));
            }
        }
        $this->error(('参数错误'));
    }


    //取消或者删除订单
    public function cancelAndDelOrder(): void
    {
        $order_id = $this->request->param('order_id');
        $option_type = $this->request->param('option_type'); //操作类型
        $order = $this->model->findOrEmpty($order_id);
        if ($order->isEmpty()) {
            $this->error(('未找到订单信息'));
        }
        switch ($option_type) {
            case 'del':
                $order->delete();
                break;
            case 'cancel':
                $order->status = PublicStatus::PUBLIC_STATUS_TWO;
                $order->save();
                break;
            default:
                break;
        }
        $this->success('操作成功');
    }
}
